package com.zhejiangzhengyuan.municipal_common.utils.LED;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeviceUtils {





    /**
     * 将传进来的 Document 解析
     *
     * @param document
     * @return
     */
    public static List<Map<String, String>> GetDeviceData(Document document) {
        if (document == null) {
            System.out.println("document 数据为空");
            return null;
        }
        Elements trs = document.select("table").select("tr");
        List<Map<String, String>> strMap = DataParse(trs.eachText());
        return strMap;
    }



    /**
     * 解析数据(对原数据进行分割操作)
     *
     * @param list
     * @return
     */
    public static List<Map<String, String>> DataParse(List<String> list) {
        if (list != null && list.size() > 0) {
            List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
            String[] split = list.toString().split(",");
            for (int i = 0; i < split.length; i++) {
                if (split[i].contains("设备")) {  //todo 含有中文的直接跳过
                    continue;
                }
                split[i] = split[i].replace("]", "");//todo 替代
                System.out.println(">>>:" + split[i]);
                Map<String, String> map = new HashMap<String, String>();
                String[] sp = split[i].split(" ");//todo 以空格分割
                for (int j = 0; j < sp.length; j++) {
                    if (sp[j] == null) {
                        continue;
                    }
                    if (sp[j].contains(":")) {  //todo 含：为设备的MAC地址
                        map.put("MAC", sp[j]); //  todo  后面看情况需要改！！！！
                    } else {
                        map.put("DeviceCode", sp[j]);
                    }
                }
                mapList.add(map);//todo  添加到集合list
            }
            return mapList;
        }
        return null;
    }


    public static void main(String[] args) throws Exception {
        Document document = Jsoup.connect("http://218.75.49.82:9999/ListenSdkService/v1.0.0/deviceMain").get();
        //Document document = Jsoup.parse(str);
        List<Map<String, String>> mapList = DeviceUtils.GetDeviceData(document);
        System.out.println(mapList);
        for (Map<String, String> map : mapList) {
            String mac = map.get("MAC");
            String deviceCode = map.get("DeviceCode");
            System.out.println(deviceCode);
        }
    }
}
